package com.lbl.third.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbl.third.common.CustomException;
import com.lbl.third.dto.DishDto;
import com.lbl.third.enytity.Dish;
import com.lbl.third.enytity.DishFlavor;
import com.lbl.third.mapper.DishMapper;
import com.lbl.third.service.DishFlavorService;
import com.lbl.third.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService {
         @Autowired
         private DishFlavorService dishFlavorService;

    /**
     * 新整菜品同时保存对应的口味
     * @param dishDto
     */

    @Override
    @Transactional                //操作俩个表，开启事务
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品进本信息
        this.save(dishDto);

        //获取菜品Id
        Long id = dishDto.getId();

        //菜品口味信息
        List<DishFlavor> dishFlavors= dishDto.getFlavors();

        //用stream流将菜品口味信息中加入刚获取的菜品id，并转为list类型（foreach方法也可）
        dishFlavors= dishFlavors.stream().map((item) -> {
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味信息
        dishFlavorService.saveBatch(dishFlavors);
    }


    /**
     * 根据id查询菜品信息和对应口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto GetByIdWithFlavor(Long id) {
        //查询菜品基本信息，从Dish表查询
        Dish dish = this.getById(id);
        //创建dishDto对象
        DishDto dishDto=new DishDto();
        //将dish数据copy到dishDto上
        BeanUtils.copyProperties(dish,dishDto);

        //条件构造器
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.eq(DishFlavor::getDishId,id);
        //执行条件查询
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        ///将查询出的口味信息放到DishDto中
        dishDto.setFlavors(list);

        return dishDto;

    }


    /**
     * 更新菜品信息，同时更新对应口味信息
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {

        //更新Dish表信息
        this.updateById(dishDto);

        //清空当前菜品对应的口味信息--dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper);


        //添加当前提交过来的口味数据--dish-flavors表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        //用stream流将菜品口味信息中加入刚获取的菜品id，并转为list类型（foreach方法也可）
        flavors= flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }


    /**
     * 删除菜品
     * @param ids
     */
    @Override
    public void removeDish(List<Long> ids) {

        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        //添加过滤条件
        queryWrapper.eq(Dish::getStatus,1);
        queryWrapper.in(Dish::getId,ids);
        int count=this.count(queryWrapper);

        //如果不能删除抛出异常
        if(count>0){
            throw new CustomException("当前选项中有菜品正在出售，无法删除");
        }
        this.removeByIds(ids);

    }
}
